﻿using RpcHelper;
using RpcModel;
using RpcSync.Collect;
using RpcSync.Collect.Model;
using RpcSync.Service.Interface;

namespace RpcSync.Service.Broadcast
{
    [RpcClient.Attr.UnityName("Root")]
    internal class LoadRootNode : IInitBroadcast
    {
        private IRemoteServerCollect _Server;
        private INodeService _NodeService;
        public LoadRootNode(IRemoteServerCollect server, INodeService nodeService)
        {
            _NodeService = nodeService;
            _Server = server;
        }
        public void InitBroadcastBody(BroadcastMsg msg, MsgSource source, ref BroadcastBody body)
        {
            List<long> nodes = new List<long>();
            msg.TypeVal.ForEach(a => _NodeService.Load(a, nodes));
            if (nodes.Count == 0)
            {
                throw new ErrorException("rpc.sync.node.null");
            }
            body.ServerId = this._Server.GetServerId( msg.RegionId,nodes);
        }
    }
}
